// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "DBManager/Repository/HDF5/Input/Table/TableSummaryRepository.h"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/IRepository/Input/Table/ITableSummaryRepository.h"
#include "DBManager/Repository/HDF5/Base/HDF5RepositoryMacro.h"

HDF5_REG_REPOSITORY (TableSummaryRepository)

using namespace SG::DBManager::HDF5;

using namespace SG::DBManager;
using namespace SG::DataStructure::FEM;

TableSummaryRepository::TableSummaryRepository (HDF5Database& database)
    : SingleRepositoryBase<SG::DBManager::SummaryInfo<SG::DataStructure::FEM::TableTypes>> (database) {};

void TableSummaryRepository::InitialDatasetInfo ()
{
    RegisterTableInfo<SG::DataStructure::Common::Id> ("/OWN/INDEX/INPUT/TABLE/SUMMARY", IndexDataset);
    RegisterTableInfo<SG::DBManager::SummaryInfo<SG::DataStructure::FEM::TableTypes>> ("/OWN/INPUT/TABLE/SUMMARY/Summary", MainDataset);
    CreateDatasetInFile ();
}

void TableSummaryRepository::InitDataset ()
{
    SingleRepositoryBase<SG::DBManager::SummaryInfo<SG::DataStructure::FEM::TableTypes>>::InitDataset ();

    auto insertData = [=] () {
        // 触发持久化信号
        GetDatabase ().GetHDF5Hub()->m_tableSummarySignal ();

        // 遍历所有index表，进行数据的读取
        for (auto&& i : GetDatabase ().GetHDF5Hub()->m_tableTypeWithIndexPathMapping)
        {
            if (GetDatabase ().GetFile ()->exist (i.second))
            {
                std::vector<SG::DataStructure::Common::Id> idList;
                GetDataset (i.second).read (idList);

                std::vector<SG::DBManager::SummaryInfo<SG::DataStructure::FEM::TableTypes>> elementSummarys;

                elementSummarys.reserve (idList.size ());

                for (size_t index = 0; index < idList.size (); index++)
                {
                    elementSummarys.emplace_back (idList[index], i.first, index);
                    // SGInfo << idList[index] << " " << i.first << " " << index;
                }

                Add (elementSummarys);
            }
        }
    };

    if (GetMainDatasetInfo ().GetRowCount () == 0)
    {
        insertData ();
        SortData ();
    }
}

SG::DataStructure::FEM::TableTypes TableSummaryRepository::GetTableType (SG::DataStructure::Common::Id Eid)
{
    SG::DBManager::SummaryInfo<SG::DataStructure::FEM::TableTypes> data;  // NOLINT(misc-const-correctness)
    if (FindById (Eid, data) == SG::DataStructure::Common::Status::STATUS_Success)
    {
        return data.m_type;
    }

    return SG::DataStructure::FEM::TableTypes::TABLETYPE_UnKonwn;
};

/// @brief 类型为`type`的单元的数量
std::size_t TableSummaryRepository::GetTableSize (SG::DataStructure::FEM::TableTypes type)
{
    // 便利所有index表，进行数据的读取
    for (auto&& i : GetDatabase ().GetHDF5Hub()->m_tableTypeWithIndexPathMapping)
    {
        if (i.first == type)
            if (GetDatabase ().GetFile ()->exist (i.second))
            {
                return GetDataset (i.second).getElementCount ();
            }
    }

    return 0;
};